package parkingManage.utils;

import java.util.List;
import java.util.Scanner;

import Util.TestUtil;
import parkingManage.dao.ParkingDao;
import parkingManage.dao.impl.ParkingDaoImpl;
import parkingManage.model.ParkingModel;

public class ParkingUtils {

	static Scanner sc = new Scanner(System.in);

	/**
	 * 查询全部车位信息
	 */
	public void selectAll() {
		int k = 1;

		ParkingDao park = new ParkingDaoImpl();
		List<ParkingModel> parks = park.selectAll();

		System.out.println("============全部车位信息============");

		for (ParkingModel pk : parks) {
			info(pk);
			if (k % 2 == 0) {
				System.out.println();
			}
			k++;
		}
	}

	/**
	 * 根据车位号查询车位信息
	 */
	public void selectByLot() {
		System.out.println("请输入车位号：");
		int lotCode = TestUtil.abnormal();

		ParkingDao parking = new ParkingDaoImpl();
		ParkingModel park = parking.selectByLot(lotCode);

		info(park);
	}

	/**
	 * 车位详细信息
	 * 
	 * @param pk
	 */
	public void info(ParkingModel pk) {
		if (pk == null) {
			System.out.println("没有查到该车位信息！");
			return;
		}
		if (pk.getLotCode() < 10) {
			System.out.print("车位号:0" + pk.getLotCode() + "--");
		} else {
			System.out.print("车位号:" + pk.getLotCode() + "--");
		}
		System.out.print(pk.getStatus() + "\t");

	}

	/**
	 * 判断这个车位是否已被占用
	 * 
	 * @param lotCode
	 * @return
	 */
	public boolean judge(int lotCode) {
		ParkingDao parkingDao = new ParkingDaoImpl();
		List<ParkingModel> list = parkingDao.selectAll();

		for (ParkingModel park : list) {
			if (park.getLotCode() == lotCode && park.getStatus().equals("占用"))
				return false;
		}
		return true;
	}

	public void start() {
		while (true) {
			System.out.println("===================================");
			System.out.println("请选择：");
			System.out.println("1-查询所有车辆信息");
			System.out.println("2-根据车牌号查询车辆信息");
			System.out.println("3-退出");
			System.out.println("===================================");

			int key = TestUtil.abnormal();
			if (key == 3)
				break;

			switch (key) {
			case 1:
				selectAll();
				break;
			case 2:
				selectByLot();
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			sc.nextLine();
		}
	}
}
